package flc.ast.fragment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.CameraActivity;
import flc.ast.activity.NarrateActivity;
import flc.ast.activity.PlayLocalActivity;
import flc.ast.activity.PlayUriActivity;
import flc.ast.activity.RecommendActivity;
import flc.ast.activity.SelVideoActivity;
import flc.ast.adapter.NarrateHomeAdapter;
import flc.ast.adapter.RecommendAdapter;
import flc.ast.databinding.FragmentHomeBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FastClickUtil;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private RecommendAdapter recommendAdapter;
    private NarrateHomeAdapter narratedAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        mDataBinding.ivCamera.setOnClickListener(this);
        mDataBinding.ivPlayFormat.setOnClickListener(this);
        mDataBinding.ivPlayUri.setOnClickListener(this);
        mDataBinding.ivPlayLocal.setOnClickListener(this);
        mDataBinding.tvRecommendMore.setOnClickListener(this);
        mDataBinding.tvNarrateMore.setOnClickListener(this);
        mDataBinding.rvRecommendList.setLayoutManager(new GridLayoutManager(mContext,3));
        recommendAdapter=new RecommendAdapter();
        recommendAdapter.setOnItemClickListener(this);
        mDataBinding.rvRecommendList.setAdapter(recommendAdapter);
        mDataBinding.rvNarrateList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
        narratedAdapter=new NarrateHomeAdapter();
        narratedAdapter.setOnItemClickListener(this);
        mDataBinding.rvNarrateList.setAdapter(narratedAdapter);
    }

    @Override
    protected void onClickCallback(View v) {
        if (FastClickUtil.isFastClick()) return;
        switch (v.getId()){
            case R.id.ivCamera:
                startActivity(CameraActivity.class);
                break;
            case R.id.ivPlayFormat:
                startActivity(SelVideoActivity.class);
                break;
            case R.id.ivPlayUri:
                startActivity(PlayUriActivity.class);
                break;
            case R.id.ivPlayLocal:
                startActivity(PlayLocalActivity.class);
                break;
            case R.id.tvRecommendMore:
                startActivity(RecommendActivity.class);
                break;
            case R.id.tvNarrateMore:
                startActivity(NarrateActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    protected void initData() {
        getRecommendData();
        getNarrateData();
    }

    private void getRecommendData(){
        StkResApi.getTagResourceList(this, AppConfig.RECOMMEND_DATA, StkResApi.createParamMap(0, 3),false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        recommendAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getNarrateData(){
        StkResApi.getTagResourceList(this, AppConfig.NARRATE_DATA, StkResApi.createParamMap(0, 3),false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        narratedAdapter.setList(result);
                    }
                }
            }
        });
    }

}